package com.park.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.park.web.base.baseOperation.BaseService;
import com.park.web.dao.basSystemManageMapper;
import com.park.web.dao.basSystemResourceMapper;
import com.park.web.model.domain.basBase;
import com.park.web.model.domain.basSystemManage;
import com.park.web.model.domain.basSystemResource;
import com.park.web.model.dto.basSystemResourceDto;
import com.park.web.model.vo.basSystemLeftMenuVo;
import com.park.web.model.vo.basSystemResourceVo;
import com.park.web.service.basSystemResourceService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class basSystemResourceServiceImpl extends BaseService<basSystemResource> implements basSystemResourceService {

    @Autowired
    basSystemResourceMapper ibasSystemResourceMapper;

    @Autowired
    basSystemManageMapper ibasSystemManageMapper;

    @Override
    public List<basSystemManage> getMenuTree(String UserID) {
        List<basSystemManage> systemManage = ibasSystemManageMapper.QuerySystemManage();

        List<basSystemResource> resourceList = UserID.equals("462889db5770adda015770b15c4e000d")? SelectMenuByAdmin(): SelectMenuByUserID(UserID);
        return getMenuTree(systemManage,resourceList);
    }

    @Override
    public List<basSystemResource> SelectMenuByUserID(String UserID) {
        return ibasSystemResourceMapper.SelectMenuByUserID(UserID);
    }

    @Override
    public List<basSystemResource> SelectMenuByAdmin() {
        return ibasSystemResourceMapper.SelectMenuByAdmin();
    }

    // 获取系统模块菜单集合(左菜单)
    private List<basSystemManage> getMenuTree(List<basSystemManage> systemList,List<basSystemResource> list) {
        List<basSystemManage> menu = new ArrayList<basSystemManage>();
        for (basSystemManage item:systemList) {
            List<basSystemResource> resourcesList = list.stream().filter(t ->t.getSystemManageId() !=null && t.getSystemManageId().equals(item.getId())).collect(Collectors.toList());
            if(resourcesList.size() > 0){
                item.setResourceList(getResourceTree(list,"SYS"));
                menu.add(item);
            }
        }
        return menu;
    }

    //递归获取菜单结构
    private List<basSystemResource> getResourceTree(List<basSystemResource> list, String parent)
    {
        List<basSystemResource> menuList = list.stream().filter(t -> t.getParentCode().equals(parent)).collect(Collectors.toList());
        for (basSystemResource root : menuList) {
            root.setResourceList(getResourceTree(list, root.getResourceCode()));
        }
        return menuList;
    }

    public List<basSystemResource> selectByParentCode(String parentCode) {
        return ibasSystemResourceMapper.selectByParentCode(parentCode);
    }

    @Override
    public basSystemResourceVo queryResourceTree() {
        return getTree();
    }

    @Override
    public List<basSystemResource> queryModuleResources(basSystemResourceDto entity) {
        return ibasSystemResourceMapper.queryModuleResources(entity);
    }

    @Override
    public PageInfo<basSystemResource> queryResourcesList(basSystemResourceDto entity) {
        PageHelper.startPage(entity.getPage(),entity.getPageSize());
        var actionList = ibasSystemResourceMapper.queryResourcesList(entity);
        return new PageInfo<>(actionList);
    }

    // 根节点
    private basSystemResourceVo getTree(){
        basSystemResourceVo  rootResource = new basSystemResourceVo();

        List<basSystemResource>   resources =  ibasSystemResourceMapper.queryResourceTree();

        basSystemResource root = resources.stream().filter(t-> t.getResourceType() == -1).findFirst().get();// collect(Collectors.toList());
        if(root != null ){
            rootResource.setExpanded(true);
            rootResource.setDownChecked(true);
            rootResource.setChecked(false);
            rootResource.setHasChildren(true);
            rootResource.setOrgSystemPath("-1");
            rootResource.setDataType("-1");
            rootResource.setImageUrl("");
            rootResource.setFullName(root.getResourceName());
            rootResource.setDataCode(root.getResourceCode());
            rootResource.setModel(root);
            rootResource.setItems(getModulesTree(resources));

        }
        return rootResource;
    }

    // 系统模块
    private List<basSystemResourceVo> getModulesTree(List<basSystemResource> resources){
        List<basSystemResourceVo> modulesResource = new ArrayList<>();
        List<basSystemManage> manages =  ibasSystemManageMapper.QuerySystemManage();
        for (basSystemManage item :manages) {
            basSystemResourceVo node = new basSystemResourceVo();
            node.setId(item.getId());
            node.setModel(item);
            node.setFullName(item.getSystemName());
            node.setDataCode(item.getSystenCode());
            node.setDataType("SYSTEM");
            node.setExpanded(true);
            node.setDownChecked(true);
            node.setChecked(false);
            node.setHasChildren(true);
            List<basSystemResourceVo> childNode = getChildTree(resources.stream().filter(t-> t.getSystemManageId() != null && t.getSystemManageId().equals(item.getId())).collect(Collectors.toList()), "SYS");
            node.setItems(childNode);
            modulesResource.add(node);
        }
        return modulesResource;
    }

    // 递归循环构建树结构
    private List<basSystemResourceVo> getChildTree(List<basSystemResource> resourceList,String parentCode)
    {
        List<basSystemResourceVo> childTree = new ArrayList<>();
        List<basSystemResource> resource = resourceList.stream().filter(t-> t.getParentCode().equals(parentCode)).collect(Collectors.toList());
        if(resource.size() >0){
            for (basSystemResource item :resource) {
                basSystemResourceVo node = new basSystemResourceVo();
                node.setId(item.getId());
                node.setModel(item);
                node.setFullName(item.getResourceName());
                node.setDataCode(item.getResourceCode());
                node.setImageUrl(item.getImgUrl());
                node.setOrgSystemPath(parentCode);
                node.setExpanded(true);
                node.setDownChecked(true);
                node.setChecked(false);
                node.setHasChildren(true);
                node.setItems(getChildTree(resourceList,item.getResourceCode()));
                childTree.add(node);
            }
        }
        return childTree;
    }
}
